#include "filepreview.h"
#include "ui_filepreview.h"

#include <QMessageBox>
#include <QByteArray>
#include <QDebug>

filepreview::filepreview(QString fileurl, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::filepreview),
    file(fileurl)
{
    ui->setupUi(this);
    if(!file.open(QIODeviceBase::ReadWrite))
    {
        QMessageBox::critical(this, "严重错误", "文件打开失败");
        return;
    }
    QByteArray text = file.readAll();

    ui->textEdit->setText(QString(text));

}

filepreview::~filepreview()
{
    delete ui;
}

void filepreview::on_pushButton_back_clicked()
{
    this->parentWidget()->show();
    if(file.isOpen())
    {
        file.close();
    }
    this->close();
}


void filepreview::on_pushButton_save_clicked()
{
    QString newtext = ui->textEdit->toPlainText();
    if(file.isOpen())
    {
        file.close();
    }
    if(!file.open(QIODeviceBase::ReadWrite | QIODeviceBase::Truncate))
    {
        QMessageBox::critical(this, "严重错误", "保存失败");
        return;
    }
    file.write(newtext.toUtf8());
    file.close();
    QMessageBox::information(this, "提示", "保存成功");
}

